package com.erp.myFragment


import android.content.SharedPreferences
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.erp.mclass.UnCeHandler
import com.erp.util.Myutil
import com.erp.util.Sputil

/**
 * Created by Administrator on 2017/11/10/010.
 * 作者：刘洋
 */
abstract class BaseKFragment : Fragment(){
    var fragmentname = ""
    lateinit var editor: SharedPreferences.Editor//sp储存器
    lateinit var sp: SharedPreferences//sp提取器
    var isVisibl = false//当前Fragment是否可见
    var isInitView = false//是否与View建立起映射关系
    var isFirstLoad = true//是否是第一次加载数据
    lateinit var convertView: View
    var isTiao = false
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        convertView = inflater.inflate(getLayoutId(), container, false)
        UnCeHandler.fragment = fragmentname
        editor = Sputil.editor("erp")
        sp = Sputil.sp("erp")

        isInitView = true
//        initData()
//        lazyLoadData()
        return convertView
    }

    override fun onResume() {
        super.onResume()
        lazyLoadData()
    }
    //加载页面布局文件
    protected abstract fun getLayoutId(): Int
    //加载要显示的数据
    protected abstract fun initData()

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        if (isVisibleToUser) {
            isVisibl = true
            UnCeHandler.fragment = fragmentname
            lazyLoadData()

        } else {
            isVisibl = false
        }
        super.setUserVisibleHint(isVisibleToUser)
    }

    fun lazyLoadData() {
        if (!isFirstLoad || !isVisibl || !isInitView) {
            return
        }
        isFirstLoad = false
        initData()
    }

    open fun setData(data : String){

    }


}